package com.gexin.controller.xapp.product;

import com.gexin.common.model.ApiResponse;
import com.gexin.core.interceptor.XAppSafeInterceptor;
import com.gexin.core.web.AppController;
import com.gexin.core.web.XAppController;
import com.gexin.model.product.ProductType;
import com.gexin.model.product.Products;
import com.gexin.model.user.UserCollect;
import com.gexin.service.product.ProductService;
import com.jfinal.aop.Before;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

@Before(XAppSafeInterceptor.class)//统一错误拦截
@ControllerBind(controllerKey = "/xapp/product")
public class ProductController  extends AppController {

	/**
	 * 获取产品列表
	 */
	public void index(){
		//获取产品列表
		List<ProductType> productList = ProductService.dao.findProductList();
		success(productList);
	}

	/**
	 * 获取产品详情
	 */
	public void productInfo(){
		Integer productId = getParaToInt("productId");
		Products products = ProductService.dao.findProductInfo(productId);
		success(products);
	}

	/***
	 * 添加到我的收藏
	 */
	public void addCollection(){
		Integer productId = getParaToInt("product_id");
		ApiResponse apiResponse = ProductService.dao.addCollection(productId , getAuthUser());
		renderJson(apiResponse);
	}

	/**
	 * 我的收藏列表
	 */
	public void collectionList(){
		if(getAuthUser()==null){
			fail("当前用户未登录!");
			return;
		}
		Page<UserCollect> collectionList = ProductService.dao.findCollectionList(getPageIndex() , getPageSize() , getAuthUser());
		renderJson(collectionList);
	}


}
